<template>
	<view id="detail">
		<view class="wrap" v-if="visible">
			<view class="bg_img" @click=" previewImage(cook.pic)">
				<image :src="cook.pic"></image>
			</view>
			<view class="desc">
				<view class="name">
					<view class="title">
						<text user-select=true>{{cook.name}}</text>
					</view>
					
					<view class="time">
						<u-icon :size="32" name="clock" custom-prefix="custom-icon"></u-icon>
						<text>{{cook.cookingtime}}</text>
					</view>
					
				</view>
				
				<view class="text">
					
					<view style="font-weight: bold;font-size: 30rpx;padding:10rpx 0;">
						菜单说明
					</view>
					
					<view class="tag">
					
						<text user-select=true>{{cook.tag}}</text>
					</view>
					
					<view class="content">
						<u-read-more show-height="200" toggle=true ref="uReadMore">
								<u-parse :selectable="true" :html="cook.content"></u-parse>
							</u-read-more>
					</view>
					
					
				</view>
				
				<view class="material">
					<view style="font-weight: bold;font-size: 30rpx;padding:10rpx 0;">食材清单</view>
					<view class="base">
						<view style="font-weight: bold;padding:10rpx 0;">主料</view>
						<text user-select=true v-for="(material, index) in cook.material" v-if="material.type==1">
							{{material.mname}}:{{material.amount}}
						</text>
					</view>
					<view style="height:1rpx;width: 100%;background-color: #f8f8f8;margin:20rpx 0"></view>
					<view class="other">
						<view style="font-weight: bold;padding:10rpx 0;">辅料</view>
						<text user-select=true v-for="(material, index) in cook.material" v-if="material.type==0">
							{{material.mname}}:{{material.amount}}
						</text>
					</view>
					
				</view>
				
			</view>
			
			<view class="">
				<view style="font-weight: bold;font-size: 30rpx;padding:10rpx 0;">
					烹饪步骤：
				</view>
				<view class="step" v-for="(item, index) in cook.process">
					<view style="font-weight: bold;padding:10rpx 0;">
						步骤{{index+1}}
					</view>
					
					<view class="pcontent">
						<text user-select=true>{{item.pcontent}}</text>
					</view>
					
					<view class="img" @click="previewImage(item.pic)">
						<image :src="item.pic"></image>
					</view>
				</view>
			</view>
			
		</view>
		
		<view v-else>
			出错啦...
		</view>
	</view>
</template>

<script>
	const baseUrl = "https://way.jd.com/jisuapi/";
	const myKey = "d841f29c61d4b1178380910774222055";
	export default {
		data() {
			return {
				cook: {},
				cookId: 0,
				visible: true
			}
		},
		mounted() {
			// this.getDetailById();
		},
		onLoad(option) {
			this.cookId = option.id;
			this.getDetailById();
		},
		methods:{
			getDetailById() {
				let that = this;
				uni.request({
					url: baseUrl+"detail?appkey="+myKey+"&id="+that.cookId,
					method:"get",
					success(res) {
						console.log(res.data)
						if(res.data.result){
							let result = res.data.result.result;
							console.log(result)
							that.cook = result;
						}else{
							that.visible = false;
						}
						
						that.$nextTick(() => {
											that.$refs.uReadMore.init();
										})
						
					},
					fail(err) {
						
					}
				})
			},
			/**
			 * @param {Object} pic
			 * 预览图片
			 */
			 previewImage(pic) {
				 uni.previewImage({
					urls: [pic],
					longPressActions: {
						itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			 }
		}
	}
</script>

<style lang="scss" scoped>
	#detail{
		padding:30rpx;
		background-color: #f7f7f7;
		.wrap{
			.bg_img{
				image{
					// height:400rpx;
					width: 100%;
					border-radius: 10rpx;
				}
			}
			
			.desc{
				.name{
					background-color: #fff;
					display: flex;
					padding:10rpx;
					.title{
						font-size: 32rpx;
						font-weight: bold;
					}
					
					.time{
						margin-left:20rpx;
						color: #999;
						text{
							vertical-align: top;
						}
					}
				}
				
			
				
				.text{
					background-color: #fff;
					margin-top:20rpx;
					padding:0 20rpx;
					
					.tag{
						padding:10rpx 0;
					}
					.content{
						padding:10rpx 0;
					}
				}
				
				
				.material{
					background-color: #fff;
					margin-top:10rpx;
					padding: 20rpx;
					text{
						display: inline-block;
						min-width: 150rpx;
					}
				}
			}
			
			
			
			.step{
				background-color: #fff;
				margin-top:20rpx;
				padding:20rpx;
				.pcontent{
					padding-bottom: 20rpx;
				}
				.img{
					image{
						height:300rpx;
						width: 70%;
						border-radius: 10rpx;
					}
				}
			}
		}
	}
</style>
